#include <rtdef.h>
#include <rtthread.h>
#include "RTC_8025T.h"
#include "hc32_ll_clk.h"
#include "cpuHc32f460.h"
#include "hc32_ll_fcg.h"
#include "hc32_ll_adc.h"
#include "hc32_ll_gpio.h"
#include "dev_i2c.h"
/*
*8025T写函数
*/
void RTC_8025T_Write(struct rt_i2c_bus_device *i2c_bus, rt_uint8_t *buf, rt_uint32_t num )
{
    struct rt_i2c_msg msg;
    
    LL_PERIPH_WE(LL_PERIPH_GPIO);
    msg.addr  = RTC_WRITE >> 1;  // 地址右移1位！:cite[5]
    msg.flags = RT_I2C_WR;  // 写标志
    msg.buf   = buf;
    msg.len   = num;
    
    if (rt_i2c_transfer(i2c_bus, &msg, 1) != 1) {
        rt_kprintf("Write failed\n");
    }
    LL_PERIPH_WP(LL_PERIPH_GPIO);
}

/*
*8025T读函数
*/
void RTC_8025T_Read(struct rt_i2c_bus_device *i2c_bus, rt_uint8_t *read_buf, rt_uint32_t Rev_num )
{
    struct rt_i2c_msg msgs[2];
    uint8_t temp[1]={0x00};
    
    LL_PERIPH_WE(LL_PERIPH_GPIO);
    // 步骤1：发送待读寄存器地址
    msgs[0].addr  = RTC_WRITE >> 1;
    msgs[0].flags = RT_I2C_WR;
    msgs[0].buf   = temp;
    msgs[0].len   = 1;
    
    // 步骤2：读取数据
    msgs[1].addr  = RTC_READ >> 1;
    msgs[1].flags = RT_I2C_RD;  // 读标志
    msgs[1].buf   = read_buf;
    msgs[1].len   = Rev_num;
    
    if (rt_i2c_transfer(i2c_bus, msgs, 2) != 2) {
        rt_kprintf("Read failed\n");
    }
    LL_PERIPH_WP(LL_PERIPH_GPIO);
}

/*
*8025T初始化
*/
void RTC_8025T_init()
{
    uint8_t  temp[2] ={0x0F,0x60};
    RTC_8025T_Write(RTC_8025Tdev, temp,2 );

}
